package com.longyiqun.apitest;

import com.alibaba.fastjson.JSON;
import com.google.gson.*;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

import static com.longyiqun.apitest.GetApiResult.getBodyWithSign;
import static io.restassured.RestAssured.given;
@Component
public class login  {

    @Autowired
    private testmapper testmapper;
    public static login login;
    @PostConstruct
    public void init(){
        login = this;
        login.testmapper = this.testmapper;
    }
    public   int  loginSet(String url,String body,String env,String model){

        //我打系统
        if (url.indexOf("CM_WorkbenchLogin/WD_COMM_AskVCode") != -1){
            Object succesResponse = JSON.parse(body);    //先转换成Object
            Map map = (Map)succesResponse;         //Object强转换为Map
            String loginName = map.get("LoginName").toString();
            String CODE_URL = "CM_WorkbenchLogin/WD_COMM_AskVCode";
            String Login_URL = "CM_WorkbenchLogin/WD_COMM_WorkbenchLogin";
            String findCodeURL = TestServer.getURL(model, env) + CODE_URL;
            YLog.RedGreen("","---------");
            JsonObject jsonObjectCode = GetApiResult.getBodyWithSign(body, "", "");
            YLog.MagentYellow("js:",jsonObjectCode.toString());

            try {
                final Response response = given().contentType(ContentType.JSON).body(jsonObjectCode.toString()).post(findCodeURL);
                // 获取response中的验证码
                JsonParser jp = new JsonParser();
                JsonElement jeReturn = jp.parse(response.body().asString());
                String CodeAndMobileJson = jeReturn.getAsJsonObject().get("Data").toString();
                String LOGIN_BODY;
                LOGIN_BODY = CodeAndMobileJson;
                // 获取TOKEN的URL
                String getTokenURL = TestServer.getURL(model, env) + Login_URL;

                // 获取真正的BODY体
                JsonObject jsonObjectTokenUID = getBodyWithSign(LOGIN_BODY, "", "");

                // 获取response
                final Response response1 = given().contentType(ContentType.JSON).body(jsonObjectTokenUID.toString()).post(getTokenURL);
                JsonElement jeReturn1 = jp.parse(response1.body().asString());
                System.out.println(jeReturn1);
                // 返回Token和UID
                String Token = jeReturn1.getAsJsonObject().get("Data").getAsJsonObject().get("Token").getAsString();
                String   UID = jeReturn1.getAsJsonObject().get("Data").getAsJsonObject().get("EmployeeID").toString();
               TestTokenEntity testTokenEntity = new TestTokenEntity();
                testTokenEntity.setToken(Token);
                testTokenEntity.setEnv(env);
                testTokenEntity.setLoginName(loginName);
                testTokenEntity.setUid(UID);
                System.out.println(testTokenEntity);
                int result =  login.testmapper.setTokenUid(testTokenEntity);
                YLog.MagentYellow("result:",Integer.toString(result));

//                JDBCImpl jdbc = new JDBCImpl();
//                String result = jdbc.SetTokenUid(loginName,Token,UID,env);
            } catch (Exception ex) {
                return 0;
            }
            return  1;
        }
        //计返费系统
        else if (url.indexOf("VCodeManager/GetVCode") != -1){
            Object succesResponse = JSON.parse(body);    //先转换成Object
            Map map = (Map)succesResponse;         //Object强转换为Map
            String loginName = map.get("SPhone").toString();
            String CODE_URL = "VCodeManager/GetVCode";
            String Login_URL = "JFF_Login/JFF_LOGN_WebLogin";
            String findCodeURL = TestServer.getURL(model, env) + CODE_URL;
            JsonObject jsonObjectCode = GetApiResult.getBodyWithSign(body, "", "");
            try {

                final Response response = given().contentType(ContentType.JSON).body(jsonObjectCode.toString()).post(findCodeURL);

                // 获取response中的验证码
                JsonParser jp = new JsonParser();
                JsonElement jeReturn = jp.parse(response.body().asString());
                String code = jeReturn.getAsJsonObject().get("Data").toString();
                String JFF_BODY;
                JFF_BODY = "{\"Mobile\":\""+loginName+"\",\"VerifyCode\":"+code+"}";



                String getTokenURL = TestServer.getURL(model, env) + Login_URL;
                JsonObject jsonObjectTokenUID = getBodyWithSign(JFF_BODY, "", "");
                // 获取response
                final Response response1 = given().contentType(ContentType.JSON).body(jsonObjectTokenUID.toString()).post(getTokenURL);
                JsonElement jeReturn1 = jp.parse(response1.body().asString());
                // 返回Token和UID
                String Token = jeReturn1.getAsJsonObject().get("Data").getAsJsonObject().get("Token").getAsString();
                String   UID = jeReturn1.getAsJsonObject().get("Data").getAsJsonObject().get("UserID").toString();
                TestTokenEntity testTokenEntity = new TestTokenEntity();
                testTokenEntity.setToken(Token);
                testTokenEntity.setEnv(env);
                testTokenEntity.setLoginName(loginName);
                testTokenEntity.setUid(UID);
                int result = login.testmapper.setTokenUid(testTokenEntity);

//                JDBCImpl jdbc = new JDBCImpl();
//                String result = jdbc.SetTokenUid("",Token,UID,env);
            } catch (Exception ex) {
                System.out.println("error");
                return 0;
            }
            try{
                Thread.sleep(1000);
            }catch (Exception e){}
            return 1;
        }else {
            return 1;
        }
    }

    public  String loginGet(String loginName,String env){
        TestTokenEntity testTokenEntity = new TestTokenEntity();
        testTokenEntity.setEnv(env);
        testTokenEntity.setLoginName(loginName);
         testTokenEntity = login.testmapper.findTokenUid(testTokenEntity);
//        JDBCImpl jdbc = new JDBCImpl();
        String token_uid = testTokenEntity.getToken()+","+testTokenEntity.getUid();
        System.out.println(token_uid+"========");
        return token_uid;
    }
}
